package com.jgx.dao;
import com.jgx.bean.employee502;
import com.jgx.util.Dbutil509;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class employDao502{
    public List<employee502> findAll() {
        Connection connection = Dbutil509.getConnection();
        if (connection == null) {
            System.out.println("数据库连接失败");
            return new ArrayList<>(); // 返回空列表，表示连接失败
        }
        List<employee502> eList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from employee";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                employee502 employee = new employee502();
                employee.setEmployeeNumber(resultSet.getInt(1));
                employee.setRoomNumber(resultSet.getInt(2));
                employee.setName(resultSet.getString(3));
                employee.setGender(resultSet.getString(4));
                employee.setContact(resultSet.getString(5));
                employee.setCleantime(resultSet.getString(6));
                eList.add(employee);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Dbutil509.closeAll(resultSet, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for(employee502 employee : eList) {
            System.out.println(employee.toString());
        }
        return eList;
    }

    public void deleteByid(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "delete from employee where EmployeeNumber = ?";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Dbutil509.closeAll(null, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(employee502 employee)  {
        Connection connection = null;
        PreparedStatement statement =null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "INSERT INTO employee(RoomNumber,Name,Gender,Contact,CleaningTime) VALUES(?,?,?,?,?)";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            statement.setInt(1, employee.getRoomNumber());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,employee.getName());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,employee.getGender());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4,employee.getContact());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5, employee.getCleantime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void update(employee502 employee){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "UPDATE employee SET RoomNumber=?,Name=?,Gender=?,Contact=?,CleaningTime=? where EmployeeNumber=?";
        try {
            statement=connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(6,employee.getEmployeeNumber());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,employee.getRoomNumber());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,employee.getName());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,employee.getGender());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4,employee.getContact());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5,employee.getCleantime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
